<?php
/**
 * News block
 * @author Kirill A Egorov 2011 DVelum project
 */
class Block_News extends Block_Abstract
{
	protected $_template = 'news.php';
	protected $_count = 5;
	const CACHE_KEY = 'block_news_data';
	
	protected function _collectData()
	{
		$cache = Application::dataCache();
		if($cache && $data = $cache->load(self::CACHE_KEY)){
			return $data;
		}
		
		$data = Model_News::getInstance()->getListVc(
			array(
				'sort'=>'news_date',
				'direction'=>'ASC',
				'start'=>0,
				'limit'=>$this->_count
			),
			array(
				'published'=>1
			),
			false,
			array('id','news_date','title')
		);	

		if($cache){
			$cache->save($data , self::CACHE_KEY);
		}
		return $data;
	}
	/**
	 * (non-PHPdoc)
	 * @see Block_Abstract::render()
	 */
	public function render()
	{
		$tpl = new Template();
		$tpl->config = $this->_config;
		$tpl->data = $this->_collectData();
		$tpl->lang = $this->_lang;
		return $tpl->render(Application::getTemplatesPath() . $this->_template);
	}
}